function ia = initialalignment(vmr, fmr)
% initialalignment  - perform the automatic IA
%
% FORMAT:       ia = initialalignment(vmr, fmr)
%
% Input fields:
%
%       vmr         VMR object
%       fmr         FMR object
%
% Output fields:
%
%       ia          TRF object with IA matrix

% Version:  v0.7f
% Build:    8110521
% Date:     Nov-05 2008, 9:00 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin ~= 2 || ...
    numel(vmr) ~= 1 || ...
   ~isBVQXfile(vmr, 'vmr') || ...
    numel(fmr) ~= 1 || ...
   ~isBVQXfile(fmr, 'fmr')
    error( ...
        'BVQXtools:BadArgument', ...
        'Bad or missing argument.' ...
    );
end

% generate ia
ia = bless(BVQXfile('new:trf'), 1);

% get coordinate frames
fmrc = fmr.CoordinateFrame;
vmrc = vmr.CoordinateFrame;

% coordinate systems
fmrs = fmr.CoordinateSystem;
vmrs = vmr.CoordinateSystem;
if fmrs ~= 1
    % TODO
end
if vmrs ~= 1
    % TODO
end

% convention
fmrrad = strcmpi(fmr.Convention(1), 'r');
if fmrrad == (vmr.Convention ~= 0)
    % TODO
end

% compute IA matrix
tfm = inv(vmrc.Trans) * fmrc.Trans;
tfm(4, :) = [0, 0, 0, 1];

% other interpretation of systems
tfm(:, 3) = -tfm(:, 3);

% some other settings
ia.CoordinateSystem = vmrc;
ia.NSlicesFMRVMR = fmr.NrOfSlices;
ia.SlThickFMRVMR = fmr.SliceThickness;
ia.SlGapFMRVMR = fmr.GapThickness;
ia.CreateFMR3DMethod = 3;
ia.AlignmentStep = 1;

% vmr transformations applied
if vmr.NrOfPastSpatialTransformations > 0
else
    ia.ExtraVMRTransf = 0;
    ia.ExtraVMRTransfValues = eye(4);
end
ia.SourceFile = fmr.FilenameOnDisk;
ia.TargetFile = vmr.FilenameOnDisk;

% store transformation matrix
ia.TFMatrix = tfm;
